cc.Class({
    extends: cc.Component,

    properties: {
        bg1: cc.Node,
        bg2: cc.Node,
        snail: cc.Node,
        speed: 100,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 获取重置触发坐标点
        this.triggerX = -this.bg1.width;
        // 设置蜗牛动画
        this.snailCrawl();
    },

    snailCrawl () {
        // 设置蜗牛动画
        let snailAnim = this.snail.getComponent(cc.Animation);
        let state = snailAnim.play();
        state.repeatCount = Infinity;
        state.speed = this.speed / 50;
    },

    update (dt) {
        // 背景移动
        this.bg1.x -= dt * this.speed;
        this.bg2.x -= dt * this.speed;

        // 重置
        if (this.bg1.x <= this.triggerX) 
            this.bg1.x = this.bg2.x+this.bg1.width;
        else if (this.bg2.x <= this.triggerX) 
            this.bg2.x = this.bg1.x+this.bg1.width;
    },
});
